//
//  HelloWorldLayerExtension.m
//  demo_3
//
//  Created by 才 陈 on 12-2-24.
//  Copyright (c) 2012年 sjtu. All rights reserved.
//

#import "HelloWorldLayer.h"

@implementation HelloWorldLayer(Extension)

-(void) initExtension
{
    SCREEN_WIDTH = winSize.width;
    SCREEN_HEIGHT = winSize.height;
    
    [self setOpacity:0];
}

-(void) addScoreListBoard
{
    scoreListBoardLayer = [NGSScoreListBoard node];
    [self addChild:scoreListBoardLayer];
    [scoreListBoardLayer setVisible:NO];
}

-(void)addBowl
{
    midBowl = [BowlOfNooldModelLayer newBowlOfNoodle:BOWL_SCALING];
    leftBowl = [BowlOfNooldModelLayer beefNoodle:BOWL_SCALING];
    rightBowl = [BowlOfNooldModelLayer newBowlOfNoodle:BOWL_SCALING];
    hiddenBowl = [BowlOfNooldModelLayer newBowlOfNoodle:BOWL_SCALING];
    
    [self addChild:midBowl z:BOWL_LAYER_Z_INDEX tag:BOWL_TAG];
    [self addChild:leftBowl z:BOWL_LAYER_Z_INDEX tag:BOWL_TAG];
    [self addChild:rightBowl z:BOWL_LAYER_Z_INDEX tag:BOWL_TAG];
    [self addChild:hiddenBowl z:BOWL_LAYER_Z_INDEX tag:BOWL_TAG];
    
    int zoomedBowlWidth = BOWL_WIDTH*BOWL_SCALING;
    int leftBowlX = DISPLY_WIDTH_OF_SIDE_BOWL-zoomedBowlWidth/2;
    int midBowlX = SCREEN_WIDTH/2;
    int rightBowlX = SCREEN_WIDTH-DISPLY_WIDTH_OF_SIDE_BOWL+zoomedBowlWidth/2;
    int hiddenBowlX = 3*SCREEN_WIDTH/2-2*DISPLY_WIDTH_OF_SIDE_BOWL+zoomedBowlWidth;
    
    [leftBowl runAction:[CCPlace actionWithPosition:ccp(leftBowlX,BOWL_DISPLAY_POSITION_Y)]];
    [midBowl runAction:[CCPlace actionWithPosition:ccp(midBowlX,BOWL_DISPLAY_POSITION_Y)]];
    [rightBowl runAction:[CCPlace actionWithPosition:ccp(rightBowlX,BOWL_DISPLAY_POSITION_Y)]];
    [hiddenBowl runAction:[CCPlace actionWithPosition:ccp(hiddenBowlX, BOWL_DISPLAY_POSITION_Y)]];
    
    [NGSUtils printPoint:midBowl.position startWith:@"mid_bowl_position"];
    [NGSUtils printRect:midBowl.boundingBox startWith:@"mid_bowl_init_bounding_box"];
}

-(void)bowlMoveActionCallback
{
    int zoomedBowlWidth = BOWL_WIDTH*BOWL_SCALING;
    int hiddenBowlPosX = -DISPLY_WIDTH_OF_SIDE_BOWL-zoomedBowlWidth;
    int leftBowlPosX = DISPLY_WIDTH_OF_SIDE_BOWL-zoomedBowlWidth/2;
    int midBowlPosX = SCREEN_WIDTH/2;
    int rightBowlPosX = SCREEN_WIDTH-DISPLY_WIDTH_OF_SIDE_BOWL+zoomedBowlWidth/2;
    /*
    int leftBowlMoveToX = -DISPLY_WIDTH_OF_SIDE_BOWL-zoomedBowlWidth;
    int hiddenBowlMoveToX = SCREEN_WIDTH-DISPLY_WIDTH_OF_SIDE_BOWL+zoomedBowlWidth/2;
    int midBowlMoveToX = DISPLY_WIDTH_OF_SIDE_BOWL-zoomedBowlWidth/2;
    int rightBowlMoveToX = SCREEN_WIDTH/2;
    */
    
    //move action destination position
    float bowlMoveDuration = BOWL_MOVE_DURATION;
    CGPoint leftBowlPos = ccp(leftBowlPosX, BOWL_DISPLAY_POSITION_Y);
    CGPoint hiddenBowlPos = ccp(hiddenBowlPosX, BOWL_DISPLAY_POSITION_Y);
    CGPoint midBowlPos = ccp(midBowlPosX, BOWL_DISPLAY_POSITION_Y);
    CGPoint rightBowlPos = ccp(rightBowlPosX, BOWL_DISPLAY_POSITION_Y);
    /*
    CGPoint leftBowlDestPos = ccp(leftBowlMoveToX, BOWL_DISPLAY_POSITION_Y);
    CGPoint hiddenBowlDestPos = ccp(hiddenBowlMoveToX, BOWL_DISPLAY_POSITION_Y);
    CGPoint midBowlDestPos = ccp(midBowlMoveToX, BOWL_DISPLAY_POSITION_Y);
    CGPoint rightBowlDestPos = ccp(rightBowlMoveToX, BOWL_DISPLAY_POSITION_Y);
    */
    
    //put back the hidden bowl to its initial position.
    int hiddenBowlX = 3*SCREEN_WIDTH/2-2*DISPLY_WIDTH_OF_SIDE_BOWL+zoomedBowlWidth;
    //清空面碗中已有的面片及食材
    [hiddenBowl prepareNewBowlOfNoodle];
    hiddenBowl.position = ccp(hiddenBowlX, BOWL_DISPLAY_POSITION_Y);
    
    //先行置换各个面碗的引用
    [self switchBowl];
    
    //actions
    //[self printBowlPosition:YES];
    [leftBowl runAction:[CCMoveTo actionWithDuration:bowlMoveDuration position:leftBowlPos]];
    [midBowl runAction:[CCMoveTo actionWithDuration:bowlMoveDuration position:midBowlPos]];
    [rightBowl runAction:[CCMoveTo actionWithDuration:bowlMoveDuration position:rightBowlPos]];
    [hiddenBowl runAction:[CCMoveTo actionWithDuration:bowlMoveDuration position:hiddenBowlPos]];
    //[self printBowlPosition:NO];

    //switch the bowl reference
    
}

-(void) switchBowl
{
    BowlOfNooldModelLayer *tmpBowl = leftBowl;
    //CCSprite *tmpBowl = leftBowl;
    leftBowl = midBowl;
    midBowl = rightBowl;
    rightBowl = hiddenBowl;
    hiddenBowl = tmpBowl;
    NSLog(@"hidden bowl position set to (%f,300)",hiddenBowl.position.x);

}

-(void) printBowlPosition:(BOOL) beforeOrAfter
{
    NSLog((beforeOrAfter==YES)?@"Before:\n":@"After\n");
    NSLog(@"left bowl position: (%f,300)",leftBowl.position.x);
    NSLog(@"mid bowl position: (%f,300)",midBowl.position.x);
    NSLog(@"right bowl position: (%f,300)",rightBowl.position.x);
    NSLog(@"hidden bowl position: (%f,300)",hiddenBowl.position.x);
}

-(void) update:(ccTime)dt
{
    CGRect bowlHitRect = [self getBowlHitRect];
    
    float flyFlourX = flyFlour.position.x;
    float flyFlourY = flyFlour.position.y;
    
    if(flyFlourY>300)
    {
        [self removeChild:flyFlour cleanup:YES];
    } else
    {
        CGRect flyFlourRect = CGRectMake(flyFlourX, flyFlourY, 10, 10);
        
        if(CGRectIntersectsRect(bowlHitRect, flyFlourRect))
        {
            [self removeChild:flyFlour cleanup:YES];
            [self showNumber:_numberOfFlour];
        }
        
    }
}

-(CGRect) getBowlHitRect
{
    //get the hit bowl
    CCLayer *hitBowl =  _numberOfFlour%10==0?rightBowl:midBowl;
    //CCSprite *hitBowl = _numberOfFlour%10==0?rightBowl:midBowl;
    if(_numberOfFlour==0) hitBowl = midBowl;
    
    //bowl scaled width and height
    int hitBowlWidth = BOWL_WIDTH*BOWL_HIT_RECT_SCALE_X;
    int hitBowlHeight = BOWL_WIDTH*BOWL_HIT_RECT_SCALE_Y;
    
    //bowl hit rect start position
    CGPoint bowlPosition = hitBowl.position;
    int hitRectX = bowlPosition.x-hitBowlWidth/2;
    int hitRectY = bowlPosition.y+hitBowlHeight;
    
    //return the bowl rect
    return CGRectMake(hitRectX, hitRectY, hitBowlWidth, hitBowlHeight);
}

-(void) addBackgoundLayer
{
    backGroundLayer = [CCLayer node];
    CCSprite *backgroundPicture = [CCSprite spriteWithFile:@"background.jpg"];
    backgroundPicture.anchorPoint = CGPointMake(0, 0);
    backgroundPicture.scale = BACKGROUND_SPRITE_SACLE;
    [backGroundLayer addChild:backgroundPicture];
    
    [self addChild:backGroundLayer z:BACKGROUND_LAYER_Z_INDEX];
}

//抛物线    -Himi
//mSprite：需要做抛物线的精灵
//startPoint:起始位置
//endPoint:中止位置
//dirTime:起始位置到中止位置的所需时间
-(void) bezierTo:(CCSprite *)mSprite direction:(CGPoint)direction durTime:(float)time callback:(CCAction *)callbackAction
{
    CGPoint startPoint = mSprite.position;
    CGPoint endPoint = [self getBezierToDestByDirection:direction fromPosition:startPoint];
    
    float sx = startPoint.x;
    float sy = startPoint.y;
    float ex = endPoint.x;
    float ey = endPoint.y;
    //float ex = realDest.x+50;
    //float ey = realDest.y+150;
    //int h = [flyFlour contentSize].height*0.5;
    ccBezierConfig bezier; // 创建贝塞尔曲线
    bezier.controlPoint_1 = ccp(sx, sy); // 起始点
    bezier.controlPoint_2 = ccp(sx+(ex-sx)*0.6, sy+(ey-sy)*0.5+200); //控制点
    //bezier.endPosition = ccp(realDest.x-30, realDest.y+h); // 结束位置
    bezier.endPosition = ccp(ex, ey);
    
    CCScaleBy *actionScale = [CCScaleBy actionWithDuration:time scale:0.3];
    CCBezierTo *move = [CCBezierTo actionWithDuration:time bezier:bezier];
    
    CCSequence *actionMoves = [CCSequence actions:move, callbackAction, nil];
    //将两个动作封装成一个同时播放进行的动作
    CCAction * actions = [CCSpawn actions:actionMoves, actionScale, nil];
    
    [mSprite runAction:actions];
}

-(CGPoint) getBezierToDestByDirection:(CGPoint)direction fromPosition:(CGPoint)pos
{
    //初始化起始结束点坐标
    float sx = pos.x;
    float sy = pos.y;
    float ey = BOWL_DISPLAY_POSITION_Y+BOWL_HEIHT*BOWL_SCALING/3;
    float ex = sx;
    
    float dy = ey - sy;
    float cotangentOfDirection = direction.x/direction.y;
    float dx = dy*cotangentOfDirection;
    
    return CGPointMake(ex+dx, ey);
}

@end
